package com.hangim.pce.controller;

import com.hangim.pce.model.BaseDrug;
import com.hangim.pce.model.vo.request.DrugVo;
import com.hangim.pce.service.BaseDrugService;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.github.pig.common.constant.CommonConstant;
import com.github.pig.common.util.Query;
import com.github.pig.common.util.R;
import com.github.pig.common.web.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.Map;

/**
 * <p>
 * 基本药品表 前端控制器
 * </p>
 *
 * @author hongjiliang
 * @since 2018-10-19
 */
@RestController
@RequestMapping("/baseDrug")
public class BaseDrugController extends BaseController {
    @Autowired
    private BaseDrugService baseDrugService;


    /**
     * 通过ID查询
     *
     * @param id ID
     * @return BaseDrug
     */
    @GetMapping("/{id}")
    public R<BaseDrug> get(@PathVariable Long id) {
        return new R<>(baseDrugService.selectById(id));
    }


    /**
     * 分页查询信息
     *
     * @param params 分页对象
     * @return 分页对象
     */
    @RequestMapping("/page")
    public Page page(@RequestParam Map<String, Object> params) {
        params.put(CommonConstant.DEL_FLAG, CommonConstant.STATUS_NORMAL);
        return baseDrugService.selectPage(new Query<>(params), new EntityWrapper<BaseDrug>().like("name", (String) params.get("name")));
    }

    /**
     * 添加
     *
     * @param baseDrug 实体
     * @return success/false
     */
    @PostMapping("/add")
    public R<BaseDrug> add(@RequestBody BaseDrug baseDrug) {
        baseDrug.setCreatedAt(new Date());
        return new R<>(baseDrugService.saveDrug(baseDrug));
    }

    /**
     * 删除
     *
     * @param id ID
     * @return success/false
     */
    @DeleteMapping("/{id}")
    public R<BaseDrug> delete(@PathVariable Long id) {
        BaseDrug baseDrug = new BaseDrug();
        baseDrug.setId(id);
        baseDrug.setUpdatedAt(new Date());
        baseDrug.setActive(false);
        return new R<>(baseDrugService.deleteDrug(baseDrug));
    }

    /**
     * 编辑
     *
     * @param baseDrug 实体
     * @return success/false
     */
    @PutMapping
    public R<BaseDrug> edit(@RequestBody BaseDrug baseDrug) {
        baseDrug.setUpdatedAt(new Date());
        return new R<>(baseDrugService.saveDrug(baseDrug));
    }


    /**
     * 药品规则
     *
     * @param id 实体
     * @return ElementVo
     */
    @ResponseBody
    @GetMapping("rule/{id}")
    public R<DrugVo> getRules(@PathVariable Long id) {
        return new R<>(baseDrugService.getRules(id));
    }

    /**
     * 药品规则
     *
     * @param data 实体
     * @return success/false
     */
    @ResponseBody
    @PostMapping("/saveRules")
    public R<Boolean> saveRules(@RequestBody DrugVo data) {
        return new R<>(baseDrugService.saveRules(data));
    }


}
